/*
 *  Copyright 2011 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.db_vdumper.presenters;

import java.awt.event.ActionListener;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.opu.db_vdumper.actions.About;
import org.opu.db_vdumper.actions.AbstractAction;
import org.opu.db_vdumper.actions.DbConnect;
import org.opu.db_vdumper.actions.Dump;
import org.opu.db_vdumper.actions.Exit;
import org.opu.db_vdumper.actions.Help;
import org.opu.db_vdumper.actions.db.DbAction;
import org.opu.db_vdumper.actions.db.ExecuteQueryAction;
import org.opu.db_vdumper.actions.view.ViewCorrectAction;
import org.opu.db_vdumper.beans.ApplicationData;
import org.opu.db_vdumper.ui.MainView;

/**
 *
 * @author yura
 */
public class EventBus {

    private ApplicationData data;
    private MainView mainView;

    /** All available actions */
    private Map<String, AbstractAction> actions;

    /** Actions that fires when application closed */
    private final Set<ActionListener> listeners;

    /** Actions that depends of db connect, so value of database connect in this
     * actions must chenging each time, as change database connect in model. */
    private final Map<String, DbAction> connectActions;

    public EventBus(ApplicationData data, MainView mainView) {
        this.data = data;
        this.mainView = mainView;
        this.listeners = new HashSet<ActionListener>();
        this.actions = new HashMap<String, AbstractAction>();
        this.connectActions = DbAction.getActions();

        init();
    }

    public boolean addExitListeners(Collection<ActionListener> e) {
        return listeners.addAll(e);
    }

    /**
     *  Adding all actions to {@link #actions} map
     */
    private void init(){
        actions.put(About.MNAME, new About(mainView));
        actions.put(DbConnect.MNAME, new DbConnect(data, connectActions));
        actions.put(Dump.MNAME, new Dump(data));
        actions.put(Exit.MNAME, new Exit(listeners));
        actions.put(Help.MNAME, new Help());
        actions.putAll(ViewCorrectAction.allAvailable(mainView));
        actions.putAll(connectActions);
    }

    public AbstractAction getAction(String name){
        return actions.get(name);
    }

    public Exit getExitAction() {
        return (Exit) actions.get(Exit.MNAME);
    }

    public ExecuteQueryAction getExecuteQueryAction() {
        return (ExecuteQueryAction) actions.get(ExecuteQueryAction.MNAME);
    }

    public Collection<DbAction> dbActions(){
        return connectActions.values();
    }
}
